import utils from "./index";
import permission from "./permission";

const install = (Vue, vm) => {
	// 修改路由跳转前,判断是否登录
	Vue.prototype.$router = async (url, params = {}, type = "navigateTo") => {
		// 判断是否登录
		const isLogin = uni.getStorageSync("token");
		if (isLogin) {
			switch (type) {
				case "navigateTo":
					vm.$u.route(url, params);
					break;

				case "redirectTo":
					vm.$u.route(url, params, "redirectTo");
					break;

				case "reLaunch":
					vm.$u.route(url, params, "reLaunch");
					break;

				case "switchTab":
					vm.$u.route(url, params, "switchTab");
					break;
			}
		} else {
			const data = await permission.getUserInfo();
			const {
				code
			} = await permission.getCode();
			const loginResult = await vm.$api.wxLogin({
				code
			});
			const
				token = loginResult.data;
			uni.setStorageSync("token", token);
			// const currentPage = utils.getCurrentPage();
			// currentPage.onLoad(currentPage.options) || currentPage.onShow();
		}
	};
};

export default {
	install,
};
